/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aplicacao;

import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import infra.Locadora;
import infra.LocadoraListener;
import infra.ModeloTabela;
import modelo.Locacao;

/**
 *
 * @author Edemilson Danker
 * @author Jefferson Sidney Rossetim
 * @author Rogerio Mello Vanti
 */
public class TelaPrincipal extends JFrame implements LocadoraListener, WindowListener {

    private Locadora locadora;
    private ModeloTabela modeloTabela;
    private DefaultListModel modeloFilmes;
    private DefaultListModel modeloClientes;

    public TelaPrincipal() {
        super("Locadora - Sistemas Seguros");
        iniciarAtributos();
        initComponents();
        configurarComponentes();
    }

    private void iniciarAtributos() {
        this.modeloClientes = new DefaultListModel();
        this.modeloFilmes = new DefaultListModel();
        this.modeloTabela = new ModeloTabela();
        try {
            this.locadora = Locadora.getInstancia();
        } catch (FileNotFoundException erro) {
            JOptionPane.showMessageDialog(null, "Arquivo não encontrado durante a leitura!");
        } catch (IOException erro) {
            JOptionPane.showMessageDialog(null, "Erro de I/O durante a leitura!");
        } catch (ClassNotFoundException erro) {
            JOptionPane.showMessageDialog(null, "Classe não encontrada durante a leitura!");
        } finally {
            //codigo de recuperação
        }//fim finally
    }

    private void configurarComponentes() {
        //this.locadora.assinarDSA();
        
        
        this.lCliente.setModel(this.modeloClientes);
        this.lFilmes.setModel(this.modeloFilmes);
        
        

        this.addWindowListener(this);
        this.locadora.adicionarOuvinte(this);
        this.locadora.adicionarOuvinte(this.modeloTabela);

        carregarListaCliente();
        carregarListaFilme();
        carregarListalocacao();

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private void carregarListaCliente() {
        for (int i = 0; i < this.locadora.getClientes().size(); i++) {
            this.modeloClientes.addElement(this.locadora.getCliente(i).getNome());
        }//fim for
    }

    private void carregarListaFilme() {
        for (int i = 0; i < this.locadora.getAcervo().size(); i++) {
            this.modeloFilmes.addElement(this.locadora.getFilme(i).getTitulo());
        }//fim for
    }

    private void carregarListalocacao() {
        for (int i = 0; i < this.locadora.getLocacoes().size(); i++) {
            this.modeloTabela.adicionarLocacao(this.locadora.getLocacao(i));            
        }//fim for
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabelaLocacao = new javax.swing.JTable(this.modeloTabela);
        jPanel1 = new javax.swing.JPanel();
        bCadastrarCliente = new javax.swing.JButton();
        bRemoverCliente = new javax.swing.JButton();
        bCadastrarFilme = new javax.swing.JButton();
        bRemoverFilme = new javax.swing.JButton();
        bLocar = new javax.swing.JButton();
        bDevolver = new javax.swing.JButton();
        bDevolver1 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        lFilmes = new javax.swing.JList();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        lCliente = new javax.swing.JList();
        jPanel5 = new javax.swing.JPanel();
        jTabbedPane2 = new javax.swing.JTabbedPane();
        jPanel6 = new javax.swing.JPanel();
        bt_cadastrar_cliente = new javax.swing.JToggleButton();
        jt_cliente = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jt_nrCartao = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtCPF = new javax.swing.JTextField();
        jPanel7 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jScrollPane1.setViewportView(tabelaLocacao);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Controle", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION));

        bCadastrarCliente.setText("Cadastrar Cliente");
        bCadastrarCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bCadastrarClienteActionPerformed(evt);
            }
        });

        bRemoverCliente.setText("Remover Cliente");
        bRemoverCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bRemoverClienteActionPerformed(evt);
            }
        });

        bCadastrarFilme.setText("Cadastrar Filme");
        bCadastrarFilme.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bCadastrarFilmeActionPerformed(evt);
            }
        });

        bRemoverFilme.setText("Remover Filme");
        bRemoverFilme.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bRemoverFilmeActionPerformed(evt);
            }
        });

        bLocar.setText("Locar Filme");
        bLocar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bLocarActionPerformed(evt);
            }
        });

        bDevolver.setText("Devolver Filme");
        bDevolver.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bDevolverActionPerformed(evt);
            }
        });

        bDevolver1.setText("Verificar Cliente");
        bDevolver1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bDevolver1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(bCadastrarCliente, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE)
                    .addComponent(bRemoverCliente, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(bLocar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(bRemoverFilme, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(bCadastrarFilme, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(bDevolver, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(bDevolver1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(bCadastrarCliente)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bRemoverCliente)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bCadastrarFilme)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bRemoverFilme)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bLocar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bDevolver)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 9, Short.MAX_VALUE)
                .addComponent(bDevolver1))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Filmes", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION));

        lFilmes.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lFilmesMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(lFilmes);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3)
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Clientes", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION));

        lCliente.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lClienteMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(lCliente);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2)
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 565, Short.MAX_VALUE)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(27, 27, 27)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jTabbedPane1.addTab("Principal", jPanel4);

        jTabbedPane2.setTabPlacement(javax.swing.JTabbedPane.LEFT);

        bt_cadastrar_cliente.setText("Cadastrar");
        bt_cadastrar_cliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_cadastrar_clienteActionPerformed(evt);
            }
        });

        jt_cliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jt_clienteActionPerformed(evt);
            }
        });

        jLabel1.setText("Nome:");

        jt_nrCartao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jt_nrCartaoActionPerformed(evt);
            }
        });

        jLabel2.setText("Cartão Credito:");

        jLabel3.setText("CPF:");

        txtCPF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCPFActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel6Layout.createSequentialGroup()
                .addGap(0, 293, Short.MAX_VALUE)
                .addComponent(bt_cadastrar_cliente)
                .addGap(43, 43, 43))
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jt_cliente)
                    .addComponent(txtCPF)
                    .addComponent(jt_nrCartao, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel6Layout.createSequentialGroup()
                .addGap(43, 43, 43)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addComponent(jt_cliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtCPF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jt_nrCartao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 245, Short.MAX_VALUE)
                .addComponent(bt_cadastrar_cliente)
                .addGap(33, 33, 33))
        );

        jTabbedPane2.addTab("Cliente", jPanel6);

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 417, Short.MAX_VALUE)
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 421, Short.MAX_VALUE)
        );

        jTabbedPane2.addTab("Filme", jPanel7);

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 585, Short.MAX_VALUE)
            .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel5Layout.createSequentialGroup()
                    .addGap(0, 57, Short.MAX_VALUE)
                    .addComponent(jTabbedPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 57, Short.MAX_VALUE)))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 449, Short.MAX_VALUE)
            .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel5Layout.createSequentialGroup()
                    .addGap(0, 11, Short.MAX_VALUE)
                    .addComponent(jTabbedPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 12, Short.MAX_VALUE)))
        );

        jTabbedPane1.addTab("Cadastro", jPanel5);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jt_clienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jt_clienteActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jt_clienteActionPerformed

    private void bt_cadastrar_clienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_cadastrar_clienteActionPerformed

        if (jt_cliente.getText() != null && !jt_cliente.getText().isEmpty()) {
            this.locadora.cadastrarCliente(jt_cliente.getText(),txtCPF.getText(), jt_nrCartao.getText());
            JOptionPane.showMessageDialog(null, "Cadastro realizado com sucesso");
        } else {
            JOptionPane.showMessageDialog(null, "Preencha corretamente os campos!");
        }//fim else
                
        
        // TODO add your handling code here:
    }//GEN-LAST:event_bt_cadastrar_clienteActionPerformed

    private void jt_nrCartaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jt_nrCartaoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jt_nrCartaoActionPerformed

    private void txtCPFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCPFActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtCPFActionPerformed

    private void lClienteMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lClienteMouseClicked
        if (evt.getClickCount() == 2) {
            JOptionPane.showMessageDialog(null, this.locadora.getCliente(this.lCliente.getSelectedIndex()).toString());
        }//fim if
    }//GEN-LAST:event_lClienteMouseClicked

    private void lFilmesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lFilmesMouseClicked
        if (evt.getClickCount() == 2) {
            JOptionPane.showMessageDialog(null, this.locadora.getFilme(this.lFilmes.getSelectedIndex()).toString());
        }//fim if
    }//GEN-LAST:event_lFilmesMouseClicked

    private void bDevolverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bDevolverActionPerformed
        int indiceLocacao = this.tabelaLocacao.getSelectedRow();
        if (indiceLocacao > -1) {
            this.locadora.devolver(this.locadora.getLocacao(indiceLocacao));
            this.modeloTabela.removerLocacao(indiceLocacao);
            JOptionPane.showMessageDialog(null, "Devolução realizada com sucesso!");
        } else {
            JOptionPane.showMessageDialog(null, "Selecione uma locação da tabela!");
        }//fim else
    }//GEN-LAST:event_bDevolverActionPerformed

    private void bLocarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bLocarActionPerformed
        int indiceCliente = this.lCliente.getSelectedIndex();
        int indiceFilmes = this.lFilmes.getSelectedIndex();
        if (indiceCliente > -1) {
            if (indiceFilmes > -1) {
                if (!this.locadora.getFilme(indiceFilmes).isLocado()) {
                    this.locadora.locar(this.locadora.getCliente(indiceCliente),
                        this.locadora.getFilme(indiceFilmes));
                    this.modeloTabela.atualizar();
                    JOptionPane.showMessageDialog(null, "Locação realizada com sucesso");
                }else{
                    JOptionPane.showMessageDialog(null, "Filme indisponível!");
                }
            } else {
                JOptionPane.showMessageDialog(null, "Selecione um filme da lista!");

            }//fim else
        } else {
            JOptionPane.showMessageDialog(null, "Selecione um cliente da lista!");
        }//fim else
    }//GEN-LAST:event_bLocarActionPerformed

    private void bRemoverFilmeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bRemoverFilmeActionPerformed
        int indice = this.lFilmes.getSelectedIndex();
        if (indice > -1) {
            this.locadora.removerFilme(indice);
            this.modeloFilmes.remove(indice);
        } else {
            JOptionPane.showMessageDialog(null, "Selecione um filme da lista!");
        }//fim else
    }//GEN-LAST:event_bRemoverFilmeActionPerformed

    private void bCadastrarFilmeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bCadastrarFilmeActionPerformed
        String nome = JOptionPane.showInputDialog(null, "Digite o titulo do filme!");
        String genero = JOptionPane.showInputDialog(null, "Digite o genero do filme\nEx: comedia, infantil, animação!");
        if (nome != null && genero != null && !nome.isEmpty() && !genero.isEmpty()) {
            this.locadora.cadastrarFilme(nome, genero);
            JOptionPane.showMessageDialog(null, "Cadastro realizado com sucesso");
        } else {
            JOptionPane.showMessageDialog(null, "Preencha corretamente os campos!");
        }//fim else
    }//GEN-LAST:event_bCadastrarFilmeActionPerformed

    private void bRemoverClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bRemoverClienteActionPerformed
        int indice = this.lCliente.getSelectedIndex();
        if (indice > -1) {
            this.locadora.removerCliente(indice);
            this.modeloClientes.remove(indice);
        } else {
            JOptionPane.showMessageDialog(null, "Selecione um cliente da lista!");
        }//fim else
    }//GEN-LAST:event_bRemoverClienteActionPerformed

    private void bCadastrarClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bCadastrarClienteActionPerformed
        /**        String nome = JOptionPane.showInputDialog(null, "Digite o nome do Cliente!");
        if (nome != null && !nome.isEmpty()) {
            this.locadora.cadastrarCliente(nome);
            JOptionPane.showMessageDialog(null, "Cadastro realizado com sucesso");
        } else {
            JOptionPane.showMessageDialog(null, "Preencha corretamente os campos!");
        }//fim else  */
    }//GEN-LAST:event_bCadastrarClienteActionPerformed

    private void bDevolver1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bDevolver1ActionPerformed
        
        
       int indiceLocacao = this.tabelaLocacao.getSelectedRow();
       int indiceCliente = this.lCliente.getSelectedIndex();
       
       
       boolean ok = locadora.verificarDSA(indiceCliente, indiceLocacao);     
       
       if(ok) {
           
           JOptionPane.showMessageDialog(null, "Este cliente realmente alugou este filme!");
           
            
       } else {
           JOptionPane.showMessageDialog(null, "Este cliente não alugou este filme!");
       }
            
     
        
        
        
        
        
    }//GEN-LAST:event_bDevolver1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new TelaPrincipal();
            }
        });
    }//fim main
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bCadastrarCliente;
    private javax.swing.JButton bCadastrarFilme;
    private javax.swing.JButton bDevolver;
    private javax.swing.JButton bDevolver1;
    private javax.swing.JButton bLocar;
    private javax.swing.JButton bRemoverCliente;
    private javax.swing.JButton bRemoverFilme;
    private javax.swing.JToggleButton bt_cadastrar_cliente;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTabbedPane jTabbedPane2;
    private javax.swing.JTextField jt_cliente;
    private javax.swing.JTextField jt_nrCartao;
    private javax.swing.JList lCliente;
    private javax.swing.JList lFilmes;
    private javax.swing.JTable tabelaLocacao;
    private javax.swing.JTextField txtCPF;
    // End of variables declaration//GEN-END:variables

    @Override
    public void cadastrarCliente(String nome) {
        this.modeloClientes.addElement(nome);
    }

    @Override
    public void cadastrarFilme(String nome) {
        this.modeloFilmes.addElement(nome);
    }

    @Override
    public void cadastrarLocacao(Locacao locacao) {
    }

    @Override
    public void windowOpened(WindowEvent e) {
    }

    @Override
    public void windowClosing(WindowEvent e) {
        try {
            this.locadora.salvar();
        } catch (FileNotFoundException erro) {
            JOptionPane.showMessageDialog(null, "Arquivo não encontrado durante a escrita!");
        } catch (IOException erro) {
            JOptionPane.showMessageDialog(null, "Erro de I/O durante a escrita!");
        } finally {
            //codigo de recuperação
        }//fim finally
    }

    @Override
    public void windowClosed(WindowEvent e) {
    }

    @Override
    public void windowIconified(WindowEvent e) {
    }

    @Override
    public void windowDeiconified(WindowEvent e) {
    }

    @Override
    public void windowActivated(WindowEvent e) {
    }

    @Override
    public void windowDeactivated(WindowEvent e) {
    }
}//fim class
